<template>
  <div class="app-container">
    <div class="search-con">
      <div class="title">查询条件</div>
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="药品名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入药品名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="药品编码" prop="code">
          <el-input v-model="queryParams.code" placeholder="请输入药品编码" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="治疗类型" prop="treatmentType">
          <el-select v-model="queryParams.treatmentType" placeholder="请选择治疗类型" clearable>
            <el-option v-for="dict in mar_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="药品分类" prop="classification">
          <el-select v-model="queryParams.classification" placeholder="请选择药品分类" clearable>
            <el-option v-for="dict in medicine_classification" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="类别" prop="category">
          <el-select v-model="queryParams.category" placeholder="请选择类别" clearable>
            <el-option v-for="dict in medicine_category" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="药品剂型" prop="dosageForm">
          <el-select v-model="queryParams.dosageForm" placeholder="请选择药品剂型" clearable>
            <el-option v-for="dict in dosage_form" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="queryParams.brand" placeholder="请输入品牌" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="生产厂家" prop="manufacturers">
          <el-input v-model="queryParams.manufacturers" placeholder="请输入生产厂家" clearable @keyup.enter="handleQuery" />
        </el-form-item>
      </el-form>
      <div class="search-btn-con">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button type="info" icon="Refresh" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <div class="main-con">
      <div class="title-con">
        <div class="title">基本信息</div>
        <div class="operate-btn-con">
          <el-button @click="handleAdd" icon="Plus" v-hasPermi="['health:medicineBasic:add']">新增</el-button>
          <el-button :disabled="multiple" icon="Delete" @click="handleDelete" v-hasPermi="['health:medicineBasic:remove']">删除</el-button>
          <!-- <el-button @click="handleExport" icon="Download" v-hasPermi="['health:medicineBasic:export']">导出</el-button> -->
        </div>
      </div>
      <div class="content-con" v-loading="loading" height="calc(100% - 0.65rem)">
        <el-table v-loading="loading" :data="medicineBasicList" @selection-change="handleSelectionChange" height="calc(100% - 0.65rem)">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="index" label="序号" :index="indexMethod" width="50"></el-table-column>
          <el-table-column label="药品名称" align="center" prop="name" />
          <el-table-column label="药品编码" align="center" prop="code" width="110" />
          <el-table-column label="治疗类型" align="center" prop="treatmentType" width="150">
            <template #default="scope">
              <dict-tag :options="mar_type" :value="scope.row.treatmentType" />
            </template>
          </el-table-column>
          <el-table-column label="药品分类" align="center" prop="classification" width="180">
            <template #default="scope">
              <dict-tag :options="medicine_classification" :value="scope.row.classification" />
            </template>
          </el-table-column>
          <el-table-column label="类别" align="center" prop="category" width="110">
            <template #default="scope">
              <dict-tag :options="medicine_category" :value="scope.row.category" />
            </template>
          </el-table-column>
          <el-table-column label="药品剂型" align="center" prop="dosageForm" width="120">
            <template #default="scope">
              <dict-tag :options="dosage_form" :value="scope.row.dosageForm" />
            </template>
          </el-table-column>
          <el-table-column label="包装" align="center" prop="packaging" width="150" />
          <el-table-column label="品牌" align="center" prop="brand" width="120" />
          <el-table-column label="生产厂家" align="center" prop="manufacturers" />

          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template v-slot="scope">
              <div class="ctrl-btn d-flex">
                <el-tooltip v-for="item in operateList" :key="item.id" class="item" effect="dark" :content="item.title" placement="top">
                  <el-button :icon="item.icon" v-hasPermi="item.hasPermi" circle @click="handleOperate(item.id, scope.row)"></el-button>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          background
          layout="total,sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!-- 添加或修改药品基础信息对话框 -->
    <el-dialog :title="title" v-model="open" width="1280px" append-to-body>
      <el-form ref="medicineBasicRef" :model="form" :inline="true" :rules="rules" label-width="120px">
        <el-form-item label="药品全称" prop="name">
          <el-input v-model="form.name" placeholder="请输入药品全称" />
        </el-form-item>
        <el-form-item label="药品简称" prop="shortName">
          <el-input v-model="form.shortName" placeholder="请输入药品简称" />
        </el-form-item>
        <el-form-item label="药品编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入药品编码" />
        </el-form-item>
        <el-form-item label="药品剂型" prop="dosageForm">
          <el-select v-model="form.dosageForm" placeholder="请选择药品剂型">
            <el-option v-for="dict in dosage_form" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="治疗类型" prop="treatmentType">
          <el-select v-model="form.treatmentType" placeholder="请选择治疗类型">
            <el-option v-for="dict in mar_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="药品分类" prop="classification">
          <el-select v-model="form.classification" placeholder="请选择药品分类">
            <el-option v-for="dict in medicine_classification" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类别" prop="category">
          <el-select v-model="form.category" placeholder="请选择类别">
            <el-option v-for="dict in medicine_category" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="药品包装" prop="packaging">
          <el-input v-model="form.packaging" placeholder="请输入药品包装" />
        </el-form-item>
        <el-form-item label="包装单位" prop="packageUnit">
          <el-select v-model="form.packageUnit" placeholder="请选择包装单位">
            <el-option v-for="dict in package_unit" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生产厂家" prop="manufacturers">
          <el-input v-model="form.manufacturers" placeholder="请输入生产厂家" />
        </el-form-item>

        <el-form-item label="是否进口" prop="isImport">
          <el-select v-model="form.isImport" placeholder="请选择是否进口">
            <el-option v-for="dict in is_import" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="规格" prop="specifications">
          <el-input v-model="form.specifications" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="规格单位" prop="unit">
          <el-select v-model="form.unit" placeholder="请选择规格单位">
            <el-option v-for="dict in medical_unit" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单个含量" prop="content">
          <el-input v-model="form.content" placeholder="请输入单个含量" />
        </el-form-item>
        <el-form-item label="含量单位" prop="contentUnit">
          <el-select v-model="form.contentUnit" placeholder="请选择含量单位">
            <el-option v-for="dict in content_unit" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生产地址" style="width: 788px" prop="address">
          <el-input v-model="form.address" placeholder="请输入生产地址" />
        </el-form-item>
        <el-form-item label="功能主治" style="width: 1200px" prop="indications">
          <el-input v-model="form.indications" placeholder="请输入功能主治" />
        </el-form-item>
        <el-form-item label="贮藏" style="width: 1200px" prop="storage">
          <el-input v-model="form.storage" placeholder="请输入贮藏" />
        </el-form-item>
        <el-form-item label="药品成分" style="width: 1200px" prop="ingredients">
          <el-input v-model="form.ingredients" placeholder="请输入药品成分" />
        </el-form-item>
        <el-form-item label="用法用量" style="width: 1200px" prop="usage">
          <el-input v-model="form.usage" placeholder="请输入用法用量" />
        </el-form-item>
        <el-form-item label="性状" prop="character" style="width: 1200px">
          <el-input v-model="form.character" placeholder="请输入性状" />
        </el-form-item>
        <el-form-item label="不良反应" prop="adverseReaction" style="width: 1200px">
          <el-input v-model="form.adverseReaction" type="textarea" placeholder="请输入不良反应" />
        </el-form-item>
        <el-form-item label="备注" style="width: 1200px" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template v-if="title !== '查看药品基础信息'" #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MedicineBasic">
import { listMedicineBasic, getMedicineBasic, delMedicineBasic, addMedicineBasic, updateMedicineBasic } from '@/api/health/medicineBasic'
// eslint-disable-next-line no-unused-vars
import { require } from '@/utils/require'
const { proxy } = getCurrentInstance()
const { medicine_category, mar_type, medicine_classification, dosage_form, is_import, medical_unit, content_unit, package_unit } = proxy.useDict(
  'medicine_category',
  'mar_type',
  'medicine_classification',
  'dosage_form',
  'is_import',
  'medical_unit',
  'content_unit',
  'package_unit'
)

const medicineBasicList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')
const operateList = ref([
  { id: 'view', icon: 'View', title: '查看', hasPermi: ['health:medicineBasic:query'] },
  { id: 'edit', icon: 'Edit', title: '修改', hasPermi: ['health:medicineBasic:edit'] },
  { id: 'copy', icon: 'Link', title: '复制', hasPermi: ['health:medicineBasic:edit'] },
  { id: 'delete', icon: 'Delete', title: '删除', hasPermi: ['health:medicineBasic:remove'] }
])
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    classification: null,
    category: null,
    brand: null,
    packaging: null,
    manufacturers: null,
    treatmentType: null,
    isImport: null,
    ingredients: null,
    usage: null,
    dosageForm: null,
    code: null,
    shortName: null
  },
  rules: {
    name: [{ required: true, message: '药品全称不能为空', trigger: 'blur' }],
    shortName: [{ required: true, message: '药品简称不能为空', trigger: 'blur' }],
    // classification: [{ required: true, message: '药品分类不能为空', trigger: 'change' }],
    // category: [{ required: true, message: '类别不能为空', trigger: 'change' }],
    brand: [{ required: true, message: '品牌不能为空', trigger: 'blur' }],
    packaging: [{ required: true, message: '药品包装不能为空', trigger: 'blur' }],
    // manufacturers: [{ required: true, message: '生产厂家不能为空', trigger: 'blur' }],
    // treatmentType: [{ required: true, message: '治疗类型不能为空', trigger: 'change' }],
    dosageForm: [{ required: true, message: '药品剂型不能为空', trigger: 'change' }]
    // code: [{ required: true, message: '药品编码不能为空', trigger: 'blur' }]
  }
})

const handleOperate = (operate, row) => {
  switch (operate) {
    case 'view':
      handleView(row)
      break
    case 'edit':
      handleUpdate(row)
      break
    case 'delete':
      handleDelete(row)
      break
    case 'copy':
      handleCopy(row)
      break
    default:
      break
  }
}

const { queryParams, form, rules } = toRefs(data)

/** 查询药品基础信息列表 */
function getList() {
  loading.value = true
  listMedicineBasic(queryParams.value).then((response) => {
    medicineBasicList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 复制按钮操作 */
function handleCopy(row) {
  reset()
  const _id = row.id || ids.value
  getMedicineBasic(_id).then((response) => {
    form.value = response.data
    form.value.id = null
    open.value = true
    title.value = '复制药品基础信息'
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    classification: null,
    category: null,
    brand: null,
    packaging: null,
    manufacturers: null,
    treatmentType: null,
    isImport: null,
    ingredients: null,
    usage: null,
    dosageForm: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    remark: null,
    code: null,
    specifications: null,
    unit: null,
    address: null,
    adverseReaction: null,
    content: null,
    contentUnit: null,
    character: null,
    storage: null,
    indications: null
  }
  proxy.resetForm('medicineBasicRef')
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 分页
const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  getList()
}

//每页显示条数改变
const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  getList()
}
// 序号翻页递增
const indexMethod = (index) => {
  const nowPage = queryParams.value.pageNum //当前第几页，根据组件取值即可
  const nowLimit = queryParams.value.pageSize //当前每页显示几条，根据组件取值即可
  return index + 1 + (nowPage - 1) * nowLimit // 这里可以理解成一个公式
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

// 查看
const handleView = (row) => {
  title.value = '查看药品基础信息'
  form.value = row
  open.value = true
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加药品基础信息'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getMedicineBasic(_id).then((response) => {
    form.value = response.data
    open.value = true
    title.value = '修改药品基础信息'
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs.medicineBasicRef.validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateMedicineBasic(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addMedicineBasic(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除选中的数据项？')
    .then(function () {
      return delMedicineBasic(_ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'health/medicineBasic/export',
    {
      ...queryParams.value
    },
    `medicineBasic_${new Date().getTime()}.xlsx`
  )
}

getList()
</script>
